package cn.yunhe.insurance.service.impl;


import cn.yunhe.insurance.dao.IcarsDao;
import cn.yunhe.insurance.entity.Cars;
import cn.yunhe.insurance.entity.User;
import cn.yunhe.insurance.service.CarsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 陈晓萌
 * @date 2018/9/15 17:00
 */

@Service("carsService")
public class CarsServiceImpl implements CarsService {
    @Resource
    private IcarsDao carsDao;

    @Override
    public Page<Cars> findLikeCars(Map<String, Object> map) {
        Page page = new Page();
//        获取记录总数，赋给page的total属性

        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
      /*  page.setPageSize(pageSize);*/
//        获取当前页码
        int curPageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
        /*page.setPageNum(curPageNum);*/
//        计算出总页数
        /*int q = (int) (page.getTotal() / pageSize + (page.getTotal() % pageSize == 0 ? 0 : 1));*/
        page.setTotal(carsDao.findLikeCarsCount(map));
        /*System.out.println("总页数："+q);*/
//        启用分页
        page = PageHelper.startPage(curPageNum, pageSize);
//        查询列表
        List<Cars> carslist = carsDao.findLikeCars(map);
        return page;
    }

    @Override
    public Cars findCarsByUuid(String carUuid) {
        return carsDao.findCarsByUuid(carUuid);
    }


    @Override
    public User getUserByCardId(String userCardId) {
        return carsDao.getUserByCardId(userCardId);

    }

    @Override
    public Cars getCarById(String licenseNum) {
        return carsDao.getCarById(licenseNum);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public boolean addCars(Cars cars) {
        return carsDao.addCars(cars);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public void updateCars(Cars cars) {
        carsDao.updateCars(cars);
    }


    //删除车辆信息
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public boolean delCars(Cars cars) {
        return carsDao.delCars(cars);
    }
    /*=====================*/
    //获取车辆Id
    @Override
    public Cars getCarsById(Cars cars) {
        return carsDao.getCarsById(cars);
    }
    //修改车辆信息
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public void upCarsMessage(Cars cars) {
        carsDao.upCarsMessage(cars);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public void upCarsStatus(Cars cars) {
        carsDao.upCarsStatus(cars);
    }

    public IcarsDao getCarsDao() {
        return carsDao;
    }

    public void setCarsDao(IcarsDao carsDao) {
        this.carsDao = carsDao;
    }
}
